package com.dfk.bs.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.entity.*;
import com.dfk.bs.entity.vo.Baoxiu;
import com.dfk.bs.service.*;
import com.dfk.bs.service.user.BaoxiuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Api("房屋类型管理")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserAction {

    @Autowired
    EleappService eleappService;
    @Autowired
    HousetypeService housetypeService;
    @Autowired
    HouseService houseService;
    @Autowired
    RoomService roomService;
    @Autowired
    BaoxiuService baoxiuService;

    @Autowired
    UserService userService;


    @ApiOperation(value = "根据housecode查询roomcodes")
    @PostMapping("/roomcodes/{housecode}")
    public R roomcodes(
            @ApiParam(name = "housecode", value = "房屋门号", required = true)
            @PathVariable("housecode") String housecode) {
//        //根据housecode查询房间room
//        List<Room> roomList = roomService.list(
//                Wrappers.<Room>lambdaQuery()
//                        .eq(Room::getHousecode, housecode)
//        );

        House house = houseService.getOne(
                Wrappers.<House>lambdaQuery()
                        .eq(House::getHousecode, housecode), false

        );
        String typename = house.getTypename();
        Housetype housetype = housetypeService.getOne(
                Wrappers.<Housetype>lambdaQuery()
                        .eq(Housetype::getName, typename), false
        );
        return R.ok().message("房间显示").data("housetype", housetype);
    }

    @ApiOperation(value = "根据name查询eleappcodes")
    @PostMapping("/eleappcodes/{name}")
    public R eleappcodes(
            @ApiParam(name = "name", value = "房间名称", required = true)
            @PathVariable("name") String name) {
        //根据housecode和roomname  进行查找eleapp表  所有得电器
       /* List<Eleapp> eleappList = eleappService.list(
                Wrappers.<Eleapp>lambdaQuery()
                        .eq(Eleapp::getHousecode, housecode)
                        .eq(Eleapp::getName, name)
        );*/

        Room one = roomService.getOne(
                Wrappers.<Room>lambdaQuery()
                        .eq(Room::getName, name), false
        );
        return R.ok().message("房间显示").data("roomdata", one);
    }


    @PostMapping("/baoxiu/uploadone")
    public R uploadone(MultipartFile file, HttpServletRequest request) throws IOException {

        String fileName = file.getOriginalFilename();

        String msg = request.getParameter("msg");
        System.out.println(fileName);
        byte[] bytes = file.getBytes();
        // file.getSize();获取到上传文件的大小
        File file1 = new File("D:\\img\\" + fileName);
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        fileOutputStream.write(bytes);
        fileOutputStream.close();
        return R.ok().message("上传图片成功");
    }

    @PostMapping("/baoxiu/uploads")
    public R uploads(HttpServletRequest request) throws IOException {
        String msg = request.getParameter("msg");
        String housecode = request.getParameter("housecode");
        String eleappname = request.getParameter("eleappname");
        //根据 housecode查询业主名字
        User one = userService.getOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getHousecode, housecode), false
        );
        String name = one.getName();

        Baoxiu baoxiu = new Baoxiu();
        baoxiu.setMsg(msg);
        baoxiu.setName(name);
        baoxiu.setHousecode(housecode);
        baoxiu.setEleappname(eleappname);


        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");  //创建一个list接受前端发送的文件
        for (MultipartFile file : files) { //用foreach处理list列表中的file
            String fileName = file.getOriginalFilename();
            baoxiu.setPicture(fileName);
            byte[] bytes = file.getBytes();
            File file1 = new File("D:\\img\\" + fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(file1);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            baoxiuService.save(baoxiu);
        }
        return R.ok().message("上传图片成功");
    }


    @PostMapping("/baoxiu/show/{housecode}")
    public R showImage(@PathVariable("housecode") String housecode, HttpServletRequest request) throws IOException {

        List<Baoxiu> baoxius = baoxiuService.list(
                Wrappers.<Baoxiu>lambdaQuery()
                        .eq(Baoxiu::getHousecode, housecode)
        );
        //倒序
        Collections.reverse(baoxius);

        return R.ok().message("查看报修记录").data("baoxiu", baoxius);
    }


    @PostMapping("/updatepassword")
    public R updatePassword(@RequestBody JSONObject json) throws IOException {

        String housecode = json.getString("housecode");
        String password = json.getString("password");

        User one = userService.getOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getHousecode, housecode), false
        );
        one.setPassword(password);
        System.out.println("one............" + one);
        boolean b = userService.updateById(one);
        if (b == true) {
            return R.ok().message("修改密码成功");
        }
        return R.error().message("修改密码失败");

    }


}
